﻿#pragma execution_character_set("utf-8")
#include <QPainter>
#include <QDebug>
#include "login.h"
#include "dialog.h"
#include "ui_dialog.h"

Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);
    
    // 设置窗口背景透明
    setStyleSheet("background: transparent;");
    
    // 设置文本浏览器背景半透明
    ui->textBrowser_1->setStyleSheet("background-color: rgba(245, 245, 245, 180); border: none; padding: 10px;");
    ui->textBrowser_2->setStyleSheet("background-color: rgba(245, 245, 245, 180); border: none; padding: 10px;");
    ui->textBrowser_3->setStyleSheet("background-color: rgba(245, 245, 245, 180); border: none; padding: 10px;");
    
    // 设置其他控件样式
    ui->label->setStyleSheet("background: transparent; font-size: 18px; font-weight: bold; color: #0078D7;");
    ui->label_count->setStyleSheet("background: transparent;");
    ui->label_name->setStyleSheet("background: transparent;");
    ui->listWidget->setStyleSheet("background-color: rgba(255, 255, 255, 180); border-radius: 5px; border: 1px solid #CCCCCC;");
    ui->pushButton->setStyleSheet("background-color: rgba(0, 120, 215, 200); color: white; border-radius: 5px;");
    ui->pushButton_get->setStyleSheet("background-color: rgba(0, 120, 215, 200); color: white; border-radius: 5px;");
    ui->pushButton_user->setStyleSheet("background-color: rgba(0, 120, 215, 200); color: white; border-radius: 5px;");
    
    // 添加下拉框样式设置，解决选中一个另一个变黑的问题
    ui->comboBox->setStyleSheet("background-color: rgba(255, 255, 255, 230); border-radius: 5px; border: 1px solid #CCCCCC;");
    ui->comboBox_infor->setStyleSheet("background-color: rgba(255, 255, 255, 230); border-radius: 5px; border: 1px solid #CCCCCC;");
    
    // 启用HTML格式
    ui->textBrowser_1->setReadOnly(true);
    ui->textBrowser_2->setReadOnly(true);
    ui->textBrowser_3->setReadOnly(true);
    
    // 设置窗口标题和图标
    setWindowTitle("QT聊天室");
    setWindowIcon(QIcon("logo.ico"));
    
    socket = new QTcpSocket(this);
    socket->connectToHost("127.0.0.1",9966);
    //堆栈链接
    connect(ui->listWidget,SIGNAL(currentRowChanged(int)),ui->stackedWidget,SLOT(setCurrentIndex(int)));
    //连接提示
    connect(socket,SIGNAL(connected()),this,SLOT(connectedSlot()));
    //断提示
    connect(socket,SIGNAL(disconnected()),this,SLOT(disconnectedSlot()));
    //接受到信息槽函数
    connect(socket,SIGNAL(readyRead()),
            this,SLOT(readyReadSlot()));
    connect(ui->pushButton,SIGNAL(clicked()),this,SLOT(pushLinked()));
    //获取消息
    connect(ui->pushButton_get,SIGNAL(clicked()),this,SLOT(getLinked()));
    connect(ui->pushButton_user,SIGNAL(clicked()),this,SLOT(userLinked()));

}



Dialog::~Dialog()
{
    delete ui;
}
void Dialog::readyReadSlot()
{
    QByteArray buffer = socket->readAll();
    // QByteArray → QString
    QString text(buffer);
    // 显示
    if(text.at(0)==':')
    {
        // 处理消息记录
        ui->textBrowser_3->setHtml(ui->textBrowser_3->toHtml() + text.mid(1));
        return ;
    }

    QStringList list=text.split("/");
    if(list.size()==2)
    {
        QString text="人数：";
        text.append(list.at(1));
        ui->label_count->setText(text);
    }
    else if(list.size()==3)
    {
        QString sender = list.at(0);
        QString time = list.at(1);
        QString message = list.at(2);

        // 自己的消息靠右，他人的消息靠左
        if(sender == ui->label_name->text())
        {
            // 自己的消息 - 增大字体大小
            QString html = QString("<div style='text-align: right; margin: 12px 0;'>")
                          + "<div style='display: inline-block; background-color: #95EC69; padding: 10px; border-radius: 10px; max-width: 60%;'>"
                          + "<span style='font-size: 18px;'>" + message + "</span>"
                          + "</div><br/><span style='font-size: 14px; color: #888888;'>" + time + "</span>"
                           + "</div>";
            ui->textBrowser_1->setHtml(ui->textBrowser_1->toHtml() + html);
        }
        else
        {
            // 他人的消息 - 增大字体大小
            QString html = QString("<div style='text-align: left; margin: 12px 0;'>")
                          + "<span style='font-size: 14px; color: #888888;'>" + sender + "  " + time + "</span><br/>"
                          + "<div style='display: inline-block; background-color: #FFFFFF; padding: 10px; border-radius: 10px; max-width: 60%;'>"
                          + "<span style='font-size: 18px;'>" + message + "</span>"
                          + "</div>"
                          + "</div>";
            ui->textBrowser_1->setHtml(ui->textBrowser_1->toHtml() + html);
        }
    }
    else if(list.size()==4)
    {
        QString sender = list.at(0);
        QString receiver = list.at(1);
        QString time = list.at(2);
        QString message = list.at(3);

        // 私聊消息样式 - 增大字体大小
        QString direction = (sender == ui->label_name->text()) ? "right" : "left";
        QString bgColor = (sender == ui->label_name->text()) ? "#95EC69" : "#FFFFFF";
        QString senderInfo = (sender == ui->label_name->text()) ? "我发送给 " + receiver : receiver + " 发送给我";

        QString html = QString("<div style='text-align: %1; margin: 12px 0;'>").arg(direction)
                      + "<span style='font-size: 14px; color: #888888;'>" + senderInfo + "  " + time + "</span><br/>"
                      + QString("<div style='display: inline-block; background-color: %1; padding: 10px; border-radius: 10px; max-width: 60%;'>").arg(bgColor)
                      + "<span style='font-size: 18px;'>" + message + "</span>"
                      + "</div>"
                      + "</div>";
        ui->textBrowser_2->setHtml(ui->textBrowser_2->toHtml() + html);
    }
    else
    {

        QString text="人数：";
        text.append(list.at(1));
        ui->label_count->setText(text);
        list.removeLast();

        if(i)
        {
            ui->label_name->setText(list.at(0));
            i=0;
        }

        int nn=ui->comboBox->count();
        for(int i=nn-1;i>-1;i--)
        {
            ui->comboBox->removeItem(i);

        }
        nn=ui->comboBox_infor->count();
        for(int i=nn-1;i>-1;i--)
        {
            ui->comboBox_infor->removeItem(i);

        }
        for(int i=2;i<list.size();i++)
        {
            if(ui->label_name->text()!=list.at(i))
            {
                ui->comboBox->addItem(list.at(i));
                ui->comboBox_infor->addItem(list.at(i));
            }

        }
        ui->comboBox_infor->addItem("聊天室");
    }


}

void Dialog::pushLinked()
{
    QString text=ui->lineEdit->text();
    if(!text.isEmpty())
    {
        QByteArray buffer = text.toUtf8();
        // 发送
        socket->write(buffer);
        // 清空输入框
        ui->lineEdit->clear();
    }
}

void Dialog::getLinked()
{
    QString text;
    if(ui->comboBox_infor->currentText()=="聊天室")
    {
        text="*";

        QByteArray buffer = text.toUtf8();
        // 发送
        socket->write(buffer);
        // 清空输入框
        ui->lineEdit_uesr->clear();
    }else
    {
        text=ui->comboBox_infor->currentText();

        QByteArray buffer = text.toUtf8();
        // 发送
        socket->write(buffer);
        // 清空输入框
        ui->lineEdit_uesr->clear();

    }


}

void Dialog::userLinked()
{
    QString text=ui->comboBox->currentText();
    text.append("/").append(ui->lineEdit_uesr->text());
    QByteArray buffer = text.toUtf8();
    // 发送
    socket->write(buffer);
    // 清空输入框
    ui->lineEdit_uesr->clear();
}
void Dialog::connectedSlot()
{
    ui->label_conn->setText("已连接");
}

void Dialog::disconnectedSlot()
{
    ui->label_conn->setText("未连接");
}

// 添加绘制背景的事件处理函数
void Dialog::paintEvent(QPaintEvent *event)
{
    // 加载背景图片 - 使用相对路径
    QPixmap pixmap("backgroundpic.jpg");
    
    // 检查图片是否成功加载
    if (pixmap.isNull()) {
        qDebug() << "Failed to load background image!";
        QDialog::paintEvent(event);
        return;
    }
    
    // 缩放图片以适应窗口大小
    QPixmap scaledPixmap = pixmap.scaled(this->size(), Qt::KeepAspectRatioByExpanding, Qt::SmoothTransformation);
    // 创建画家对象
    QPainter painter(this);
    // 绘制背景图片
    painter.drawPixmap(0, 0, scaledPixmap);
    // 绘制其他部件
    QDialog::paintEvent(event);
}
